package com.cxs.plan.controller.power;

import com.cxs.plan.DTO.RoleDTO;
import com.cxs.plan.VO.RoleVO;
import com.cxs.plan.domain.HttpResult;
import com.cxs.plan.domain.entity.Role;
import com.cxs.plan.service.RoleService;
import com.cxs.plan.utils.PageUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin")
@Tag(name = "角色管理")
public class RoleController {
    @Autowired
    private PageUtil pageUtil;

    @Autowired
    private RoleService roleService;


    @GetMapping("/role")
    public HttpResult getRoleList(String name) {
        List<Role> roles = roleService.selectRoleList(name);
        return HttpResult.success(pageUtil.autoPage(roles));
    }

    @PostMapping("/role")
    public HttpResult addRole(@RequestBody @Validated RoleDTO role) {

        roleService.saveRole(role);
        return HttpResult.success();
    }

    @GetMapping("/role/{id}")
    public HttpResult getRole(@PathVariable("id") Integer id) {
        RoleVO role = roleService.selectRoleById(id);
        return HttpResult.success(role);
    }

    @PutMapping("/role/{id}")
    public HttpResult updateRole(@PathVariable("id") Integer id, @RequestBody RoleDTO role) {
        role.setId(id);
        roleService.updateRole(role);
        return HttpResult.success();
    }

    @GetMapping("/role/all")
    public HttpResult getAllRole() {
        List<Role> roles = roleService.selectAllRole();
        return HttpResult.success(roles);
    }
}
